package com.fit.dao.classes;

import java.util.Date;
import java.util.List;

import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.fit.entities.classes.Classes;
import com.fit.entities.user.User;

public class ClassesDAOImpl implements ClassesDAO {

	@Inject
	private Session session;

	@Log
	public boolean save(Classes newClasses) {
		if (newClasses != null) {
			session.save(newClasses);
			return true;
		}
		return false;
	}

	@Log
	public boolean delete(Classes classes) {
		if (classes != null) {
			session.delete(classes);
			return true;
		}
		return false;
	}

	@Log
	public boolean update(Classes classes) {
		if (classes != null) {
			session.update(classes);
			return true;
		}
		return false;
	}

	@Log
	public List findAll() {
		return session.createCriteria(Classes.class).list();
	}

	@Log
	public boolean deleteByID(Long id) {
		Classes u = findByID(id);
		if (u != null) {
			delete(u);
			return true;
		}
		return false;
	}

	@Log
	public Classes findByID(Long id) {
		if (id != null) {
			return (Classes) session.get(Classes.class, id);
		}
		return null;
	}

	@Log
	public Classes findByIdClassesAndBeginingDate(int classId, Date beginingDate) {
		if (classId == 0 || beginingDate == null) {
			return null;
		}

		if (classId==(0) || beginingDate.equals(null)) {
			return null;
		}

		List l = session.createCriteria(Classes.class)
				.add(Restrictions.eq("classId", classId))
				.add(Restrictions.eq("date", beginingDate)).list();
		Classes u = null;
		if (l != null) {
			if (l.size() > 0) {
				u = (Classes) l.get(0);
			}
		}
		return u;
	}

}
